home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / DOSGAMES / BOGGLE.ZIP / SOURCE.ZIP / DICT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-16  |  5.5 KB  |  184 lines

  1. /*****************************************************************************
  2. * Program:  DICT.CPP
  3. * Purpose:  Handles tasks related to the dictionary - lookup words,  etc.
  4. *****************************************************************************/
  5. #include "common.hpp"
  6. #include "dict.hpp"
  7.  
  8.  
  9. /*****************************************************************************
  10. * Function: Dictionary
  11. * Parms:    indx - filename of the index, wrds - filename of the wordlist
  12. * Purpose:  Opens the files necessary for word validation
  13. * Returns:  Nothing
  14. *****************************************************************************/
  15. Dictionary::Dictionary(char *indx, char *wrds)
  16. {
  17.    ndx.open(indx);
  18.    if (!ndx)
  19.    {
  20.    WinMessageBox(HWND_DESKTOP,
  21.                  HWND_DESKTOP,
  22.                 "Unable To Find Index File Needed For The Spell Check...",
  23.                 "Boggle System Error",
  24.                 0,                         /* message box id        */
  25.                 MB_NOICON | MB_OK);        /* icon and button flags */
  26.       exit(-1);
  27.    }
  28.  
  29.    Dictionary::initIndex();
  30.  
  31.    dict.open(wrds);
  32.    if (!dict)
  33.    {
  34.    WinMessageBox(HWND_DESKTOP,
  35.                  HWND_DESKTOP,
  36.                 "Unable To Find Dictionary File Needed For The Spell Check...",
  37.                 "Boggle System Error",
  38.                 0,                         /* message box id        */
  39.                 MB_NOICON | MB_OK);        /* icon and button flags */
  40.       exit(-1);
  41.    }
  42. }
  43.  
  44. /*****************************************************************************
  45. * Function: initIndex
  46. * Parms:    none
  47. * Purpose:  saves the index file into memory
  48. * Returns:  Nothing
  49. *****************************************************************************/
  50. void Dictionary::initIndex()
  51. {
  52.    char buf[50];
  53.  
  54.    // initialize the index
  55.    ndx >> NX;
  56.    for (int i=0; i<NX; i++) 
  57.    {
  58.       if (ndx >> buf) 
  59.       {
  60.          Index[i] = new char[strlen(buf) + 1];   //memory leak!!
  61.          strcpy(Index[i], buf);
  62.       }
  63.       ndx >> loc[i];
  64.    }
  65. }
  66.  
  67. /*****************************************************************************
  68. * Function: Lookup
  69. * Parms:    word - the word to verify in the dictionary
  70. * Purpose:  Find the word in the dictionary
  71. * Returns:  An index into the words file showing where the word is
  72. *           0 if word is not found
  73. *****************************************************************************/
  74. int Dictionary::Lookup(char *word)
  75. {
  76.    //char Buffer[50], *UWord = "";
  77.    char UWord[WORDLENGTH];
  78.    int i, r, found;
  79.  
  80.    UWord[0] = '\0';
  81.    found = FALSE;
  82.    ToUpper(UWord, word);
  83.  
  84.    /********************************************************************
  85.    * This section will find the word in the index that is greater
  86.    * than or equal to the word we are looking for.
  87.    ********************************************************************/
  88.    for (i = 0; i < NX; i++)
  89.       if ((r = strcmp(UWord, Index[i]) ) < 0) 
  90.          break;
  91.       else if (r == 0) 
  92.       {
  93.          strcpy(Buffer, Index[i]);
  94.          found = TRUE;
  95.          break;
  96.       }
  97.    if (found)
  98.       return i * 256;
  99.    else
  100.       if (i == NX)
  101.          return 0;
  102.       else 
  103.       {
  104.          /********************************************************************
  105.          * Once the position in the index is found, we will start
  106.          * sequentially searching that file starting at the offset given in
  107.          * the index for 256 words,  looking for our word.
  108.          ********************************************************************/
  109.          i = i - 1;
  110.          dict.clear(0);
  111.          dict.seekg(loc[i]);
  112.          for (int j = 0; j < 256; j++) 
  113.          {
  114.             dict.getline(Buffer, 50);
  115.             if ((r = strcmp(UWord, Buffer)) < 0)
  116.                return -(i * 256) - j;
  117.             else if (r == 0)
  118.                return (i * 256) + j;
  119.          }
  120.          strcpy(Buffer, Index[i+1]);
  121.          return -(i + 1) * 256;
  122.       }
  123. }
  124.  
  125. /*****************************************************************************
  126. * Function: ToUpper
  127. * Parms:    out - outstring, in - instring
  128. * Purpose:  convert a given string to upper case
  129. * Returns:  Nothing
  130. *****************************************************************************/
  131. void Dictionary::ToUpper (char *out, char *in)
  132. {
  133.    while (*in)
  134.    {
  135.       *out = toupper(*in);
  136.  
  137.       out++;
  138.       in++;
  139.    }
  140.    *out = '\0';
  141. }
  142.  
  143. /*****************************************************************************
  144. * Function: Prefix
  145. * Parms:    str
  146. * Purpose:  compares two strings
  147. * Returns:  true - strings are equal, false - otherwise
  148. *****************************************************************************/
  149. int Dictionary::prefix (char *str, char *buf)
  150. {
  151.    while (*str)
  152.    {
  153.       if (*str != *buf)
  154.          return FALSE;
  155.       str++;
  156.       buf++;
  157.    }
  158.    return TRUE;
  159. }
  160.  
  161.  
  162. /*****************************************************************************
  163. * Function: setBuffer
  164. * Parms:    str - strings to store
  165. * Purpose:  stores a string in this class
  166. * Returns:  nothing
  167. *****************************************************************************/
  168. void Dictionary::setBuffer(char *str)
  169. {
  170.    strcpy(Buffer, str);
  171. }
  172.  
  173. /*****************************************************************************
  174. * Function: GetBuffer
  175. * Parms:    none
  176. * Purpose:  returns the current stored string to the caller
  177. * Returns:  The stored string
  178. *****************************************************************************/
  179. char *Dictionary::getBuffer()
  180. {
  181.    return Buffer;
  182. }
  183.  
  184.